#ifndef __DLT645_FRAME_H__
#define __DLT645_FRAME_H__

#include <stdint.h>
#include <iostream>
#include <memory>
#include "dlt645def.h"

class CDlt645Frame
{
public:
    CDlt645Frame();
    ~CDlt645Frame();

    static CDlt645Frame* getIntanse();

    //读数据
    int  packet_readingdata_req(uint8_t* pszMeterAddr, uint8_t* pszDataFlag, uint8_t* pFrmData, int& iFrmLen,
                                    int iPrefixChNum =0 );
    int  packet_readingdata_req(uint8_t* pszMeterAddr, uint8_t* pszDataFlag, uint8_t ucBlockID, uint8_t* pFrmData,
                                    int& iFrmLen, int iPrefixChNum = 0);
    int  packet_readingdata_req(uint8_t* pszMeterAddr, uint8_t* pszDataFlag, uint8_t ucBlockID, uint8_t* pszDateTime,
                                    uint8_t* pFrmData, int& iFrmLen, int iPrefixChNum = 0);

    //读后续数据
    int  packet_readingsubseqdata_req(uint8_t* pszMeterAddr, uint8_t* pszDataFlag, uint8_t ucFrmID, uint8_t* pFrmData,
                                    int& iFrmLen, int iPrefixChNum = 0);

    //写数据(0x14)、控制指令帧(0x1C)--非标准645帧，对应标准协议的跳合闸、报警帧
    int  packet_writedata_req(st_dlt645_wrtdata_items stwrtDataitems, uint8_t* pFrmData, int& iFrmLen, int iPrefixChNum = 0);
    //数据转发帧(0x1E)
    int  packet_transmit_req(uint8_t* pszMeterAddr, uint8_t* pdata, int datalen, uint8_t* pFrmData, int& iFrmLen, int iPrefixChNum = 0);


private:
    static CDlt645Frame   mDlt645Frame;

    //读数据帧帧起始符~数据标识的组装
    int packet_readingdata_frmpart(uint8_t* pszMeterAddr, uint8_t* pszDataFlag, uint8_t ucBlockID, uint8_t* pszDateTime,
                                 uint8_t* pFrmData, int& iFrmLen, int iPrefixChNum,bool bIsSubseq=false);
    //校验码计算
    uint8_t CalcCSVal(uint8_t* pData, int iLen);

    //倒序拷贝
    int ReverseOrderCpy(unsigned char* pDest, unsigned char* pSrc, int iLen,uint8_t errcorr=0);
};

#endif